﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Collections;

namespace CooWork.Core
{
    public class BaseStruct
    {
    }


    public  class TreeNode : IEnumerable<TreeNode>
    {
        private readonly Dictionary<string, TreeNode> _childs = new Dictionary<string, TreeNode>();

        public readonly string ID;
        public TreeNode Parent { get;  set; }
        public string Tname{get;set;}

        public int Did { get; set; }
        public int? ParentId { get; set; }
        public bool IsLeaf {get;set;}

        public void allkeys(TreeNode tn ,List<int> li)
        {
            li.Add(tn.Did);
            if (tn.Count > 0)
            {
                foreach (TreeNode t in tn.Children)
                {
                    allkeys(t, li);
                }
            }
        }


        public TreeNode(string id,string tname,int did,int? parentId ,bool isLeaf)
        {
            this.ID = id;
            this.Tname = tname;
            this.Did = did;
            this.ParentId = parentId;
            this.IsLeaf = isLeaf;
        }

        public TreeNode GetChild(string id)
        {
            return this._childs[id];
        }

        public void Add(TreeNode item)
        {
            if (item.Parent != null)
            {
                item.Parent._childs.Remove(item.ID);
            }

            item.Parent = this;
            if (!this._childs.ContainsKey(item.ID))
            {
                this._childs.Add(item.ID, item);
            }
        }

        public IEnumerator<TreeNode> GetEnumerator()
        {
            return this._childs.Values.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public int Count
        {
            get { return this._childs.Count; }
        }

        public Dictionary<string, TreeNode>.ValueCollection Children
        {
            get {
                return this._childs.Values;
            }
        }

    }

}